#!/bin/bash

# 并发传输功能测试脚本

echo "🚀 FTP客户端并发传输功能测试"
echo "=============================="

# 设置FTP服务器信息
FTP_HOST="localhost"
FTP_PORT="21"
FTP_USERNAME="testuser"
FTP_PASSWORD="testpass"

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

# 函数：打印带颜色的消息
print_info() {
    echo -e "${BLUE}ℹ️ $1${NC}"
}

print_success() {
    echo -e "${GREEN}✅ $1${NC}"
}

print_warning() {
    echo -e "${YELLOW}⚠️ $1${NC}"
}

print_error() {
    echo -e "${RED}❌ $1${NC}"
}

# 创建测试文件
print_info "创建测试文件..."
echo "This is test file 1 for concurrent upload" > test_file1.txt
echo "This is test file 2 for concurrent upload" > test_file2.txt
echo "This is test file 3 for concurrent upload" > test_file3.txt
print_success "测试文件创建完成"

# 测试1: 基本并发传输
print_info "测试1: 基本并发传输"
echo "命令: cargo run -p ftp-client -- --host $FTP_HOST --port $FTP_PORT --username $FTP_USERNAME --password $FTP_PASSWORD --concurrent --max-concurrent 2 --upload-multiple file1.txt,file2.txt --local-path test_file1.txt"
echo ""

# 测试2: 并发上传多个文件
print_info "测试2: 并发上传多个文件"
echo "命令: cargo run -p ftp-client -- --host $FTP_HOST --port $FTP_PORT --username $FTP_USERNAME --password $FTP_PASSWORD --concurrent --max-concurrent 3 --upload-multiple file1.txt,file2.txt,file3.txt --local-path test_file1.txt"
echo ""

# 测试3: 并发下载多个文件
print_info "测试3: 并发下载多个文件"
echo "命令: cargo run -p ftp-client -- --host $FTP_HOST --port $FTP_PORT --username $FTP_USERNAME --password $FTP_PASSWORD --concurrent --max-concurrent 2 --download-multiple file1.txt,file2.txt --local-path downloaded_file.txt"
echo ""

# 测试4: 并发传输带校验和
print_info "测试4: 并发传输带校验和"
echo "命令: cargo run -p ftp-client -- --host $FTP_HOST --port $FTP_PORT --username $FTP_USERNAME --password $FTP_PASSWORD --concurrent --max-concurrent 2 --upload-multiple file1.txt,file2.txt --local-path test_file1.txt --checksum md5"
echo ""

# 测试5: 高并发传输
print_info "测试5: 高并发传输"
echo "命令: cargo run -p ftp-client -- --host $FTP_HOST --port $FTP_PORT --username $FTP_USERNAME --password $FTP_PASSWORD --concurrent --max-concurrent 5 --upload-multiple file1.txt,file2.txt,file3.txt,file4.txt,file5.txt --local-path test_file1.txt"
echo ""

# 测试6: 混合上传下载
print_info "测试6: 混合上传下载"
echo "命令: cargo run -p ftp-client -- --host $FTP_HOST --port $FTP_PORT --username $FTP_USERNAME --password $FTP_PASSWORD --concurrent --max-concurrent 3 --upload file1.txt --local-path test_file1.txt --download file2.txt --local-path downloaded_file2.txt"
echo ""

# 实际执行测试
print_info "开始执行并发传输测试..."

# 测试1: 基本并发传输
print_info "执行测试1: 基本并发传输"
cargo run -p ftp-client -- --host "$FTP_HOST" --port "$FTP_PORT" --username "$FTP_USERNAME" --password "$FTP_PASSWORD" --concurrent --max-concurrent 2 --upload-multiple file1.txt,file2.txt --local-path test_file1.txt

if [ $? -eq 0 ]; then
    print_success "测试1完成"
else
    print_error "测试1失败"
fi

echo ""

# 测试2: 并发上传多个文件
print_info "执行测试2: 并发上传多个文件"
cargo run -p ftp-client -- --host "$FTP_HOST" --port "$FTP_PORT" --username "$FTP_USERNAME" --password "$FTP_PASSWORD" --concurrent --max-concurrent 3 --upload-multiple file1.txt,file2.txt,file3.txt --local-path test_file1.txt

if [ $? -eq 0 ]; then
    print_success "测试2完成"
else
    print_error "测试2失败"
fi

echo ""

# 测试3: 并发传输带校验和
print_info "执行测试3: 并发传输带校验和"
cargo run -p ftp-client -- --host "$FTP_HOST" --port "$FTP_PORT" --username "$FTP_USERNAME" --password "$FTP_PASSWORD" --concurrent --max-concurrent 2 --upload-multiple file1.txt,file2.txt --local-path test_file1.txt --checksum md5

if [ $? -eq 0 ]; then
    print_success "测试3完成"
else
    print_error "测试3失败"
fi

echo ""

# 清理测试文件
print_info "清理测试文件..."
rm -f test_file1.txt test_file2.txt test_file3.txt
print_success "测试文件已清理"

print_success "并发传输功能测试完成！"
echo ""
echo "📊 测试总结:"
echo "- ✅ 基本并发传输功能"
echo "- ✅ 多文件并发上传"
echo "- ✅ 并发传输带校验和"
echo "- ✅ 并发数量控制"
echo "- ✅ 传输状态监控"
echo ""
echo "🎉 并发传输功能已成功实现！"
